home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / filename_expand.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  2.9 KB  |  102 lines

  1. //
  2. // "$Id: filename_expand.cxx,v 1.4 1999/01/07 19:17:34 mike Exp $"
  3. //
  4. // Filename expansion routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. /* expand a file name by substuting environment variables and
  27.    home directories.  Returns true if any changes were made.
  28.    to & from may be the same buffer.
  29. */
  30.  
  31. #include <FL/filename.H>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef WIN32
  35. #else
  36. # include <unistd.h>
  37. # include <pwd.h>
  38. #endif
  39.  
  40. #if defined(WIN32) || defined(__EMX__)
  41. static inline int isdirsep(char c) {return c=='/' || c=='\\';}
  42. #else
  43. #define isdirsep(c) ((c)=='/')
  44. #endif
  45.  
  46. int filename_expand(char *to,const char *from) {
  47.  
  48.   char temp[FL_PATH_MAX];
  49.   strcpy(temp,from);
  50.   const char *start = temp;
  51.   const char *end = temp+strlen(temp);
  52.  
  53.   int ret = 0;
  54.  
  55.   for (char *a=temp; a<end; ) {    // for each slash component
  56.     char *e; for (e=a; e<end && !isdirsep(*e); e++); // find next slash
  57.     const char *value = 0; // this will point at substitute value
  58.     switch (*a) {
  59.     case '~':    // a home directory name
  60.       if (e <= a+1) {    // current user's directory
  61.     value = getenv("HOME");
  62. #ifndef WIN32
  63.       } else {    // another user's directory
  64.     struct passwd *pwd;
  65.     char t = *e; *(char *)e = 0; 
  66.         pwd = getpwnam(a+1); 
  67.         *(char *)e = t;
  68.         if (pwd) value = pwd->pw_dir;
  69. #endif
  70.       }
  71.       break;
  72.     case '$':        /* an environment variable */
  73.       {char t = *e; *(char *)e = 0; value = getenv(a+1); *(char *)e = t;}
  74.       break;
  75.     }
  76.     if (value) {
  77.       // substitutions that start with slash delete everything before them:
  78.       if (isdirsep(value[0])) start = a;
  79. #if defined(WIN32) || defined(__EMX__)
  80.       // also if it starts with "A:"
  81.       if (value[0] && value[1]==':') start = a;
  82. #endif
  83.       int t = strlen(value); if (isdirsep(value[t-1])) t--;
  84.       memmove(a+t, e, end+1-e);
  85.       end = a+t+(end-e);
  86.       memcpy(a, value, t);
  87.       ret++;
  88.     } else {
  89.       a = e+1;
  90. #if defined(WIN32) || defined(__EMX__)
  91.       if (*e == '\\') {*e = '/'; ret++;} // ha ha!
  92. #endif
  93.     }
  94.   }
  95.   strcpy(to,start);
  96.   return ret;
  97. }
  98.  
  99. //
  100. // End of "$Id: filename_expand.cxx,v 1.4 1999/01/07 19:17:34 mike Exp $".
  101. //
  102.